import {TextField} from "@mui/material";
import DGDialogLayout from "../../../components/DGDialogLayout/DGDialogLayout.tsx";
import DGDrawerLayout from "../../../components/DGDrawerLayout/DGDrawerLayout.tsx";
import {ChangeEventHandler, FC, useState} from "react";
import {useNavigate} from "react-router-dom";
import DGTextView from "../../../components/DGTextView.tsx";
import DGConnect from "../../../sources/DGConnect.ts";
import {Util} from "../../../sources/Util.ts";

interface DGForgetIndexProps {
    state: boolean,
    switchView: (text: string) => void
    left?: boolean
}

const DGForgetIndex: FC<DGForgetIndexProps> = ({state, switchView, left}) => {
    // 路由跳转
    const router = useNavigate();
    // 账户信息
    const [account, setAccount] = useState("");
    // 账户错误信息
    const [error, setError] = useState({state: false, text: ""});
    // 加载状态
    const [loading, setLoading] = useState(false);


    // 关闭控制
    const handleClose = () => {
        // 关闭
        switchView("");
        // 清空信息
        setAccount("");
        // 后退
        router(-1);
    }


    // 更改账户信息
    const handleChangeAccount: ChangeEventHandler<HTMLInputElement> = (e) => {
        // 清空错误信息
        setError({state: false, text: ""});
        // 设置账号信息
        setAccount(e.target.value);
    }

    // 提交函数
    const handleSubmit = () => {
        // 清空错误信息
        setError({state: false, text: ""});
        // 判断账号是否为空
        if (account === "") return setError({state: true, text: "输入不能为空！"});
        // 开始加载
        setLoading(true);
        // 发起用户账户验证请求
        DGConnect.POST(JSON.stringify({type: "forget", username: account}), result => {
            // 关闭加载
            setLoading(false);
            // 错误信息
            if (result["data"]["code"] !== 200) return setError({
                state: true, text: result["data"]["message"] + "错误码：" + result["data"]["code"]
            });
            // 设置 SESSION
            Util.Storage.setSession("linked", result["data"]["result"]["session"]);
            // 设置 邮箱信息
            Util.Storage.setSession("FORGET-EMAIL", result["data"]["result"]["email"]);
            // 成功跳转
            switchView("forget_code");
        }, error => {
            // 关闭加载
            setLoading(false);
            // 设置错误信息
            setError({state: true, text: "遇到了一些问题，请尝试重试，或者及时联系我们！错误信息：" + error["stack"]});
        });
    }

    // DOM 节点
    const ContentView = (<>
        {/* 提示语 */}
        <DGTextView Body Bold>请输入需要找回密码的账号或者邮箱。</DGTextView>
        {/* 账号输入框 */}
        <TextField
            type="text" margin="dense" label="账号/邮箱" variant="standard"
            value={account} onChange={handleChangeAccount}
            error={error.state} helperText={error.text} disabled={loading}
            fullWidth required/>
    </>);

    // 返回组件节点
    return (<>
        {/* DGDialogLayout 样式 */}
        <DGDialogLayout
            submit={{name: "继续", click: () => handleSubmit()}}
            back={left && {name: "返回", click: () => switchView("login_index")} || undefined}
            title="找回密码" state={state} close={() => handleClose()} load={loading} PANEL AUTO>
            {ContentView}
        </DGDialogLayout>
        {/*DGDrawerLayout 样式 */}
        <DGDrawerLayout
            submit={{name: "继续", click: () => handleSubmit()}}
            more={left && {name: "返回", click: () => switchView("login_index")} || undefined}
            title="找回密码" state={state} close={() => handleClose()} flex="bottom" load={loading} PANEL AUTO>
            {ContentView}
        </DGDrawerLayout>
    </>);
}
export default DGForgetIndex;